package org.piaohao.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.piaohao.model.Line;
import org.piaohao.util.string.JsonUtil;
import org.piaohao.util.string.UUIDGenerator;

/**
 * Created on : 2013-5-4, 17:22:29
 *
 * @author piaohao
 */
public class LineController extends Controller {

    public static final String moduleName = "线路档案模块";

    public void index() {
        render("/pages/line/line.jsp");
    }

    public void list() {
        try {
            Integer start = getParaToInt("start");
            Integer limit = getParaToInt("limit");
            String orgId = (String) getSessionAttr("orgId");
            List<Line> lines = null;
            Long total = 0L;
            if (orgId.equals("0")) {
                lines = Line.dao.find("select * from t_line limit ?,?", start, limit);
                total = Db.queryLong("select count(*) from t_line");
            } else {
                lines = Line.dao.find("select * from t_line where org_id=? limit ?,?", orgId, start, limit);
                total = Db.queryLong("select count(*) from t_line where org_id=?", orgId);
            }
            renderJson(JsonUtil.printGrid(total, lines));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void add() {
        try {
            Line line = getModel(Line.class);
            String para1 = getPara("line_start_date");
            DateFormat df = new SimpleDateFormat("yy-MM-dd");
            Date date = df.parse(para1);
            line.set("line_start_date", new Timestamp(date.getTime()));
            if (line.getStr("id") == null || line.getStr("id").trim().equals("")) {
                line.set("id", UUIDGenerator.getUUID());
                line.save();
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void update() {
        try {
            Line line = getModel(Line.class);
            String para1 = getPara("line_start_date");
            DateFormat df = new SimpleDateFormat("yy-MM-dd");
            Date date = df.parse(para1);
            line.set("line_start_date", new Timestamp(date.getTime()));
            line.update();
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void delete() {
        try {
            String[] idArr = getParaValues("ids");
            for (int i = 0; i < idArr.length; i++) {
                Line.dao.deleteById(idArr[i]);
            }
            renderJson(JsonUtil.printSuccess(true, "成功"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(JsonUtil.printSuccess(false, "失败"));
        }
    }

    public void showDetail() {
        try {
            String id = getPara(0);
            Record line = Db.findFirst("select l.*,o.name orgName from t_line l left join t_organization o on l.org_id=o.id where l.id=?", id);
            setAttr("line", line);
            render("/pages/line/detail.jsp");
        } catch (Exception e) {
            e.printStackTrace();
            renderNull();
        }
    }
}
